<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button type="primary" @click="handleSynchronAll"
              >批量同步</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="是否同步" prop="is_synchro">
              <el-select
                v-model.trim="form.is_synchro"
                placeholder="是否同步"
                class="wid220"
              >
                <el-option label="所有" :value="parseInt(0)"></el-option>
                <el-option label="否" :value="parseInt(1)"></el-option>
                <el-option label="是" :value="parseInt(2)"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item class="btn_box" label="">
              <el-button class="reset" type="primary" @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table" style="min-height: 570px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" :selectable="selectInit" />
            <el-table-column
              label="名称"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="别名"
              prop="byname"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="图片" class-name="main_hei">
              <template slot-scope="scope">
                <div class="demo-image__error mainor_im">
                  <el-image
                    fit="fill"
                    :src="scope.row.pic"
                    :preview-src-list="scope.row.pics"
                  >
                    <div slot="error" class="image-slot">
                      <img :src="puzzleImg" />
                    </div>
                  </el-image>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="分类"
              prop="userNickname"
              width="180"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope"
                >{{ scope.row.category_p ? scope.row.category_p.title : "" }}|{{
                  scope.row.category ? scope.row.category.title : ""
                }}</template
              >
            </el-table-column>
            <el-table-column
              label="编码"
              prop="goods_code"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="上架状态"
              prop="status"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <span :class="scope.row.status == 1 ? 'success' : 'unpaid'">
                  {{ scope.row.status == 1 ? "上架" : "下架" }}
                </span></template
              >
            </el-table-column>
            <el-table-column label="是否同步" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span
                  :style="{
                    color: scope.row.synchro == 0 ? '#f56c6c' : '#03a98e',
                  }"
                >
                  {{ scope.row.synchro == 0 ? "未同步" : "已同步" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="150"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  v-if="scope.row.synchro == 0"
                  :type="
                    scope.row.synchro == 0 && scope.row.status == 1
                      ? 'warning'
                      : 'info'
                  "
                  @click="handleSynchron(scope.row)"
                  >同步</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 50, 100, 200]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
        <el-dialog
          title="使用以下方式将数据同步到用友"
          :visible.sync="dialogVisible"
          width="30%"
          @close="handleDiaClose"
        >
          <div class="dialog_box">
            <div class="left">计价方式：</div>
            <div class="right">
              <el-radio-group v-model="radio">
                <el-radio
                  :label="index"
                  v-for="(item, index) in type"
                  :key="index"
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </div>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleDiaComfirm"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import dateOption from "@/options/dateOption";
import { puzzleImg } from "@/api/currency";
import { mapGetters } from "vuex";
import { tplus_getGoods, tplus_synchroGoods } from "@/api/tplus/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    ...mapGetters(["avatar"]),
  },
  data() {
    return {
      dateOption,
      puzzleImg,
      dialogVisible: false,
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 更多
      moredata: false,
      // 列表数据
      list: [],
      type: [],
      radio: "",
      ids: "",

      listLoading: false,
      // 搜索
      form: {
        is_synchro: 0, //状态
      },
      createTime: [],
      check_createTime: [],
      selectArr: [],
    };
  },
  mounted() {},
  created() {
    // 列表
    this.tplus_getGoods();
  },
  methods: {
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.tplus_getGoods();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.createTime = [];
      this.check_createTime = [];
      this.tplus_getGoods();
    },

    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.tplus_getGoods();
    },
    changePage(page) {
      this.current.page = page;
      this.tplus_getGoods();
    },

    // 监听绑定全选不可勾选
    selectInit(row, index) {
      if (row.synchro == 0 && row.status == 1) {
        return true;
      } else {
        return false;
      }
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push(item.id);
      });
      console.log(this.selectArr);
    },
    handleDiaClose() {
      this.radio = "";
      this.ids = "";
    },
    // 弹出层确定
    handleDiaComfirm() {
      if (this.radio || this.radio !== null) {
        this.tplus_synchroGoods(this.ids);
        this.dialogVisible = false;
      } else {
        this.$message.warning("请选择同步计价方式");
      }
    },
    // 批量同步
    handleSynchronAll() {
      if (this.selectArr.length == 0) {
        this.$message.warning("请勾选需要同步的信息");
        return;
      }
      this.ids = JSON.stringify(this.selectArr);
      this.dialogVisible = true;
    },
    // 单条同步
    handleSynchron(scope) {
      let id = [scope.id];
      if (scope.synchro == 1) {
        this.$message.warning("本条数据已经同步");
        return;
      }
      if (scope.status != 1) {
        this.$message.warning("商品已下架，无法同步");
        return;
      }
      this.dialogVisible = true;
      this.ids = JSON.stringify(id);
    },
    tplus_synchroGoods(ids) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: ids, type: this.radio }
      );
      tplus_synchroGoods(data).then((res) => {
        if (res.errcode == 0) {
          this.tplus_getGoods();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 列表
    tplus_getGoods() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.form,
        this.current
      );
      tplus_getGoods(data).then((res) => {
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            if (e.images.length != 0) {
              e.images.map((a) => {
                a.path = this.avatar.domain + "/" + a.path;
                if (a.main == 1) {
                  e.pic = a.path;
                }
              });
              e.pics = e.images.map((s) => s.path);
            } else {
              e.pic = "";
              e.pics = [];
            }
          });
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
          this.type = res.data.type;
          this.listLoading = false;
        } else {
          this.$message.errcode(res.msg);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
  .main_num {
    .mainsu_te {
      position: relative;
      top: -1px;
    }
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: #f56c6c;
}
.order1 {
  color: #118ab2;
}
.order2 {
  color: #aacd6e;
}
.order3 {
  color: #073b4c;
}
.order4 {
  color: #cccccc;
}
// 支付状态
.pay_type0 {
  color: #f56c6c;
}
.pay_type1 {
  color: #03a98e;
}
.btn_box {
  padding-left: 35px;
}

.dialog_box {
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  .left {
    flex-grow: 0;
    flex-shrink: 0;
    margin-right: 25px;
    font-size: 16px;
  }
  .right {
    font-size: 16px;
    /deep/ .el-radio__label {
      font-size: 16px;
    }
    /deep/ .el-radio {
      margin-bottom: 10px;
    }
  }
}
</style>
